package com.yhgarden.bbs.controller.system;

import com.yhgarden.bbs.entity.system.SystemUser;
import com.yhgarden.bbs.entity.vo.user.UserLoginVo;
import com.yhgarden.bbs.model.enums.StatusCode;
import com.yhgarden.bbs.model.helper.LoginHelper;
import com.yhgarden.bbs.model.response.BaseResponse;
import com.yhgarden.bbs.service.api.system.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = {"/system/systemUser","*/system/systemUser"})
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    /**
     * 管理员登录
     * @param request
     * @param systemUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST,
            consumes = {"application/json"},
            produces = {"application/json"})
    @ResponseBody
    public BaseResponse login(HttpServletRequest request, @RequestBody UserLoginVo systemUser) throws Exception {
        BaseResponse res = systemUserService.login(request, systemUser);
        return res;
    }

    @RequestMapping(value = "/judgeLogin", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse judgeLogin(HttpServletRequest request) throws Exception {
        BaseResponse res = systemUserService.judgeLogin(request);
        return res;
    }

    /**
     * 退出登录
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse logout(HttpSession session) throws Exception {
        BaseResponse res = new BaseResponse(StatusCode.SUCCESS);
        LoginHelper.adminLogout(session);
        return res;
    }

    /**
     * 创建或者修改实体信息
     * @param systemUser
     * @return
     */
    @RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST,
            consumes = {"application/json"},
            produces = {"application/json"})
    @ResponseBody
    public BaseResponse createOrUpdate(HttpSession session, @RequestBody SystemUser systemUser) throws Exception {
        BaseResponse res = systemUserService.createOrUpdate(session, systemUser);
        return res;
    }

    @RequestMapping(value = "/getById/{systemUserId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getById(@PathVariable("systemUserId") String systemUserId) {
        BaseResponse res = systemUserService.getById(systemUserId);
        return res;
    }

    @RequestMapping(value = "/getByIdMoreDetail/{systemUserId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getByIdMoreDetail(@PathVariable("systemUserId") String systemUserId) {
        BaseResponse res = systemUserService.getByIdMoreDetail(systemUserId);
        return res;
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST,
            consumes = {"application/json"},
            produces = {"application/json"})
    @ResponseBody
    public BaseResponse query(@RequestBody SystemUser systemUser) {
        BaseResponse res = systemUserService.query(systemUser);
        return res;
    }

    @RequestMapping(value = "/queryForList", method = RequestMethod.POST,
            consumes = {"application/json"},
            produces = {"application/json"})
    @ResponseBody
    public BaseResponse queryForList(@RequestBody SystemUser systemUser) {
        BaseResponse res = systemUserService.queryForList(systemUser);
        return res;
    }

    @RequestMapping(value = "/delete/{systemUserId}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse delete(@PathVariable("systemUserId") String systemUserId) {
        BaseResponse res = systemUserService.delete(systemUserId);
        return res;
    }

}
